package com.orange.controller;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.orange.handler.AuthSchoolGroupService;
import com.orange.handler.AuthSchoolService;
import com.orange.modules.entity.expert.AuthExpertSchoolMap;
import com.orange.modules.entity.school.AuthSchool;
import com.orange.util.ReturnCode;

@Controller
@RequestMapping("/schoolGroup")
public class AuthSchoolGroupMapController extends BaseController  {

	private static final Logger logger = Logger.getLogger(AuthSchoolGroupMapController.class);
	
	@Autowired
	private AuthSchoolGroupService authSchoolGroupService;
	
	@Autowired
	private AuthSchoolService authSchoolService;
	
	/**
	 * 绑定学校
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/saveAuthSchoolGroup" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> saveAuthSchoolGroup(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try{
			String schoolId = map.get("schoolId");
			String groupId = map.get("groupId");
			if(schoolId == null || StringUtils.isBlank(schoolId)){
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"参数存在空值");
			}
			if(groupId == null || StringUtils.isBlank(groupId)){
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"参数存在空值");
			}
			AuthExpertSchoolMap sg = new AuthExpertSchoolMap();
			sg.setGroupId(groupId);
			sg.setSchoolId(schoolId);
			sg.setCreateTime(sdf.format(new Date()));
			authSchoolGroupService.addAuthSchoolGroupMap(sg);
			return toResponse(null,ReturnCode.SUCCESS,"绑定学校成功");
		}catch(Exception e){
			logger.error(e);
			return toResponse(null,ReturnCode.FAIL,"绑定学校失败");
		}
	}
	
	/**
	 * 分配专家进行材料审核
	 */
	@RequestMapping(value = "/inspectAuthSchool" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> inspectAuthSchool(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		try{
			List<AuthSchool> list = authSchoolGroupService.inspectAuthSchool();
			for (AuthSchool authSchool : list) {
				if(authSchool.getState().equals("1"))
					authSchool.setFpexpert("1");
					authSchoolGroupService.updateAuthSchoolFpexpert(authSchool.getState());
			}
			return toResponse(null, ReturnCode.SUCCESS, "分配成功");
		}catch(Exception e){
			logger.error(e);
			return toResponse(null, ReturnCode.FAIL, "分配失败");
		}
	}
}
